package com.genokon.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.genokon.entry.GeneServicePatientVariation;
import com.genokon.mapper.GenePatientVariationMapper;
import com.genokon.mapper.ServiceOrderMapper;
import com.genokon.service.OrderVarService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author by baichaoda
 * @date 2021/3/15 14:17
 */
@Service("orderVarService")
public class OrderVarServiceImpl implements OrderVarService {
    private String KEY_PRIMARY = "priPatient";
    @Resource
    ServiceOrderMapper serviceOrderMapper;
    @Resource
    GenePatientVariationMapper genePatientVariationMapper;

    /**
     * {
     *   "sampleNo":
     *   "dataType": "FilterSNP",
     *   "pathogenic": "Positive",
     *   "acmLevel": "PS1,PS2",
     *   "remark": "",
     *   "omiIds": "",
     *   "chr": "5",$
     *   "start": "96121681",$
     *   "end": "96121681",$
     *   "ref": "G",$
     *   "alt": "C",$
     *   "genoType":""  $
     *   dp:
     *   rd:
     *   ad:
     *   rs:
     *   "geneRef"
     *   "funcRef"
     * }
     * @param varData 数据Json
     * @return
     */
    @Override
    public String saveVar(String varData) throws Exception  {
        ObjectMapper jsonMapper = new ObjectMapper();
        JsonNode rootNode = jsonMapper.readTree(varData);
        String sampleNo = rootNode.path("sampleNo").asText();
        Map<String, Object> orderInfo = serviceOrderMapper.queryNowOrderBySampleNo(sampleNo);
        if (orderInfo == null){
            throw new Exception("样本编辑存在问题，无法找到相应订单");
        }

        Map<String, Object> oneVar = new HashMap<>(1);
        oneVar.put("sample_id", orderInfo.get("sampleId"));
        oneVar.put("order_id", orderInfo.get("orderId"));
        oneVar.put("chr", rootNode.path("chr").asText());
        oneVar.put("start", rootNode.path("start").asText());
        oneVar.put("end", rootNode.path("end").asText());
        oneVar.put("ref", rootNode.path("ref").asText());
        oneVar.put("alt", rootNode.path("alt").asText());
        oneVar.put("geno_type", rootNode.path("genoType").asText());

        GeneServicePatientVariation varObject = new GeneServicePatientVariation();
        String varId = getVarId(oneVar);
        varObject.setId(varId);
        varObject.setOrderId((String) orderInfo.get("orderId"));
        varObject.setSampleId((String) orderInfo.get("sampleId"));
        varObject.setAlt(rootNode.path("alt").asText());
        varObject.setChr(rootNode.path("chr").asText());
        varObject.setStart(rootNode.path("start").asText());
        varObject.setEnd(rootNode.path("end").asText());
        varObject.setRef(rootNode.path("ref").asText());
        varObject.setGenoType(rootNode.path("genoType").asText());
        varObject.setDp(rootNode.path("dp").asInt());
        varObject.setRd(rootNode.path("rd").asInt());
        varObject.setAd(rootNode.path("ad").asInt());
        varObject.setAcmLevel(rootNode.path("acmLevel").asText());
        varObject.setDataType(rootNode.path("dataType").asText());
        varObject.setGeneRef(rootNode.path("geneRef").asText());
        varObject.setFuncRef(rootNode.path("funcRef").asText());
        varObject.setRs(rootNode.path("rs").asText());
        varObject.setOmiIds(rootNode.path("omiIds").asText());
        varObject.setPathogenic(rootNode.path("pathogenic").asText());
        varObject.setRemark(rootNode.path("remark").asText());

        if(StringUtils.isEmpty(varId)){
            genePatientVariationMapper.insert(varObject);
        }else {
            genePatientVariationMapper.updateById(varObject);
        }
        return varObject.getId();
    }

    @Override
    public String changeOrderStatus(String sampleNo, int orderStatus) throws Exception {
        Map<String, Object> orderInfo = serviceOrderMapper.queryNowOrderBySampleNo(sampleNo);
        if (orderInfo == null){
            throw new Exception("样本编辑存在问题，无法找到相应订单");
        }

        if(1 == (Integer) orderInfo.get(KEY_PRIMARY)){
            serviceOrderMapper.updateOrderStatusById(orderStatus, (String) orderInfo.get("orderId"));
        }

        serviceOrderMapper.updateOrderDetailStatusById(orderStatus, (String) orderInfo.get("oderDetailId"));
        return (String) orderInfo.get("oderDetailId");
    }

    private String getVarId(Map<String, Object> params){
        GeneServicePatientVariation var = genePatientVariationMapper.selectOne(Wrappers.<GeneServicePatientVariation>query().allEq(params));
        return var == null? null: var.getId();
    }
}
